package webgamemaker.FightCanvas;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.widgetideas.graphics.client.Color;
import com.google.gwt.widgetideas.graphics.client.GWTCanvas;

import webgamemaker.Common.Callback;
import webgamemaker.Common.Tile;

public class Fighter implements Drawable, Selectable
{
	public Tile tile;
	public Team team;
	public HashMap<String, Object> param;
	public int standartX, standartY, standartDepth;//position sur le terrain en dehors des attaques,....
	public int x, y, depth;//position actuelle sur le terrain
	public int life, max_life;
	public boolean select;
	public FighterAction currentAction;
	public ArrayList<Callback> selectCallback;
	
	public Fighter(Tile tile)
	{
		this.tile = tile;
		param = new HashMap<String, Object>();
		life = 100;
		max_life = 100;
		currentAction = new FighterActionWait();
		selectCallback = new ArrayList<Callback>();
	}
	
	public void calcPosition()
	{
		currentAction.apply(this);
	}
	
	public void draw(FightCanvas canvas)
	{
		param.put("context", canvas);
		int size[] = tile.getSize();
		param.put("x", x-size[0]/2);
		param.put("y", y-size[1]);
		canvas.saveContext();
		//canvas.setGlobalCompositeOperation("lighter");
		//canvas.setGlobalAlpha(0.5);
		if(select)
			canvas.setGlobalAlpha(Math.abs((canvas.frame%12)-6)*0.1+0.4);
		tile.draw(param);
		canvas.restoreContext();
		//canvas.setStrokeStyle(new Color());
		canvas.strokeRect(x-size[0]/4,y-size[1],size[0]/2,5);
		canvas.setFillStyle(new Color(0,255,0));
		canvas.fillRect(x-size[0]/4,y-size[1],(size[0]/2)*life/max_life,5);
		canvas.setFillStyle(new Color(255,0,0));
		canvas.fillRect(x-size[0]/4+(size[0]/2)*life/max_life,y-size[1],(size[0]/2)*(1.0-(double)life/max_life),5);
	}
	
	public void addSelectCallback(Callback callback)
	{
		selectCallback.add(callback);
	}
	public void onSelect()
	{
		for(int i=0; i<selectCallback.size();i++)
			selectCallback.get(i).onCall(this);
	}

	public boolean isSelect(int x, int y) 
	{
		int size[] = tile.getSize();
		return x >= this.x-size[0]/2 && y >= this.y-size[1] && x <= this.x+size[0]/2 && y <= this.y;
	}
	
	public void selectDraw(GWTCanvas canvas)
	{
		param.put("context", canvas);
		int size[] = tile.getSize();
		param.put("x", x-size[0]/2);
		param.put("y", y-size[1]);
		tile.draw(param);
	}
}
